package com.tmt.api.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.swak.annotation.FluxService;
import com.swak.entity.Page;
import com.swak.entity.Parameters;
import com.swak.persistence.BaseDao;
import com.swak.persistence.QueryCondition;
import com.swak.service.BaseService;
import com.tmt.api.dao.UserAccountDao;
import com.tmt.api.dao.UserDao;
import com.tmt.api.entity.User;
import com.tmt.api.entity.UserAccount;
import com.tmt.api.service.UserService;

/**
 * 用户服务
 * 
 * @author: lifeng
 * @Date: 2020/3/28 11:49
 */
@FluxService(instances = 2)
public class UserServiceImpl extends BaseService<User, Long> implements UserService {

	@Autowired
	private UserDao userdao;
	@Autowired
	private UserAccountDao accountDao;

	@Override
	protected BaseDao<User, Long> getBaseDao() {
		return userdao;
	}

	@Override
	public User get(Long id) {
		return super.get(id);
	}

	@Override
	public User getByAccount(String account) {
		UserAccount uaAccount = this.accountDao.get(account);
		if (uaAccount != null) {
			User user = this.get(uaAccount.getUserId());
			user.setAccount(uaAccount);
			return user;
		}
		return null;
	}

	@Override
	@Transactional
	public User register(UserAccount account) {
		User user = new User();
		user.setName(account.getName());
		user.setHeadimg(account.getHeadimg());
		this.insert(user);
		account.setUserId(user.getId());
		this.accountDao.insert(account);
		return user;
	}

	@Override
	public Page page(User entity, Parameters param) {
		QueryCondition qc = new QueryCondition();
		return this.queryForPage(qc, param);
	}

	@Override
	public User save(User entity) {
		this.insert(entity);
		return entity;
	}
}